package com.boc.langxiang.ui.view;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.boc.langxiang.R;
import com.boc.langxiang.entity.bean.PhotoBean;
import com.boc.langxiang.ui.view.viewutil.ScaleInTransformer;

import java.util.ArrayList;
import java.util.List;

import me.crosswall.lib.coverflow.CoverFlow;
import me.crosswall.lib.coverflow.core.PagerContainer;

/**
 * Created by Li Guoxi on 2016/6/24.
 */
public class ProductPhotoPager extends RelativeLayout {

    private Context mContext;

    private ViewPager mViewPager;

    private ImageView[] mImageViews = null;

    private ImagePagerListener mImagePagerListener;
    private PagerContainer mPagerContainer;

    private PhotoAdapter mAdapter;

    private ImageView mImageView;

    private ViewGroup mGroup;

    public ProductPhotoPager(Context context) {
        super(context);
        this.mContext = context;
        init(context);
    }

    public ProductPhotoPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.photo_show_pager_layout, this);
        mPagerContainer = (PagerContainer) findViewById(R.id.pagerContainer);
        mViewPager = mPagerContainer.getViewPager();
//        mAdapter = new PhotoAdapter(new ArrayList<PhotoBean>());
        mViewPager.setClipChildren(false);
        mViewPager.setOffscreenPageLimit(6);

//        mViewPager.setAdapter(mAdapter);
        mViewPager.setPageTransformer(true, new ScaleInTransformer());
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < mImageViews.length; i++) {
                    mImageViews[i].setImageResource(R.drawable.icon_point_normal);
                }
                mImageViews[position].setImageResource(R.drawable.icon_point_disabled);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        new CoverFlow.Builder()
                .with(mViewPager)
                .scale(0.2f)
                .pagerMargin(getResources().getDimensionPixelSize(R.dimen.pager_margin))
                .spaceSize(0f)
                .build();

        mGroup = (ViewGroup) findViewById(R.id.indicator_layout);

    }

    public void setPhotos(List<PhotoBean> photoBeen, ImagePagerListener listener) {
        setImagePagerListener(listener);
        mGroup.removeAllViews();
        mImageViews = new ImageView[photoBeen.size()];
        for (int i = 0; i < photoBeen.size(); i++) {
            mImageView = new ImageView(mContext);
            mImageView.setAdjustViewBounds(true);
            mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            mImageViews[i] = mImageView;
            if (i == 0) {
                mImageViews[i].setImageResource(R.drawable.icon_point_disabled);
            } else {
                mImageViews[i].setImageResource(R.drawable.icon_point_normal);
            }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.rightMargin = 10;
            params.leftMargin = 10;
            mImageView.setLayoutParams(params);
            mGroup.addView(mImageView);
        }

        mAdapter = new PhotoAdapter(photoBeen);
        mViewPager.setAdapter(mAdapter);

//        mAdapter.getPhotoList().clear();
//        mAdapter.setPhotoList(photoBeen);
//        mAdapter.notifyDataSetChanged();

        new CoverFlow.Builder()
                .with(mViewPager)
                .scale(0.2f)
                .pagerMargin(getResources().getDimensionPixelSize(R.dimen.pager_margin))
                .spaceSize(0f)
                .build();
    }

    public ImagePagerListener getImagePagerListener() {
        return mImagePagerListener;
    }

    public void setImagePagerListener(ImagePagerListener imagePagerListener) {
        this.mImagePagerListener = imagePagerListener;
    }

    private class PhotoAdapter extends PagerAdapter {

        private List<PhotoBean> photoList;

        private List<View> mImageViewCacheList;

        public PhotoAdapter(List<PhotoBean> photoList) {
            this.photoList = photoList;
            mImageViewCacheList = new ArrayList<>();
        }

        @Override
        public int getCount() {
            return photoList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
//            String imageUrl = photoList.get(position).getImageUrl();
            PhotoBean photo = photoList.get(position);
            View rootView = null;
            ImageView imageView;
            if (mImageViewCacheList.isEmpty()) {
                rootView = LayoutInflater.from(mContext).inflate(R.layout.photo_pager_item, null);
            } else {
                rootView = mImageViewCacheList.remove(0);
            }
            imageView = (ImageView) rootView.findViewById(R.id.photo_iv);

            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mImagePagerListener.onImageClick(photoList.get(position), position, v);
                }
            });
            mImagePagerListener.displayImage(photo, imageView);
            container.addView(rootView);
            return rootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = (View) object;
            container.removeView(view);
            mImageViewCacheList.add(view);
        }

        public List<PhotoBean> getPhotoList() {
            return photoList;
        }

        public void setPhotoList(List<PhotoBean> photoList) {
            this.photoList = photoList;
        }
    }

    public interface ImagePagerListener {

        /**
         * 加载图片资源
         *
         * @param photo
         * @param imageView
         */
        void displayImage(PhotoBean photo, ImageView imageView);

        /**
         * 单击图片事件
         *
         * @param position
         * @param imageView
         */
        void onImageClick(PhotoBean info, int position, View imageView);
    }
}
